import { createRouter, createWebHistory } from 'vue-router'
import HomePage from '@/pages/HomePage.vue'
import UserRegisterPage from '@/pages/user/UserRegisterPage.vue'
import UserManagePage from '@/pages/admin/UserManagePage.vue'
import AddPicturePage from '@/pages/AddPicturePage.vue'
import PictureManagePage from '@/pages/admin/PictureManagePage.vue'
import PictureDetailPage from '@/pages/PictureDetailPage.vue'
import AddPictureBatchPage from '@/pages/AddPictureBatchPage.vue'
import SpaceManagePage from '@/pages/admin/SpaceManagePage.vue'
import AddSpacePage from '@/pages/AddSpacePage.vue'
import MySpacePage from '@/pages/MySpacePage.vue'
import SpaceDetailPage from '@/pages/SpaceDetailPage.vue'
import SearchPicturePage from '@/pages/SearchPicturePage.vue'
import SpaceAnalyzePage from '@/pages/SpaceAnalyzePage.vue'
import SpaceUserManagePage from '@/pages/admin/SpaceUserManagePage.vue'
import UserExchangeVipPage from '@/pages/UserExchangeVipPage.vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import TimeLinePage from '@/pages/TimeLinePage.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomePage,
    },
    {
      path: '/user/login',
      name: '用户登录',
      component: () => import('@/pages/user/UserLoginPage.vue'),
    },
    {
      path: '/user/register',
      name: '用户注册',
      component: UserRegisterPage,
    },
    {
      path: '/user/setting',
      name: '用户设置',
      component: () => import('@/views/SettingView.vue'),
    },
    {
      path: '/admin/userManage',
      name: '用户管理',
      component: UserManagePage,
    },
    {
      path: '/admin/pictureManage',
      name: '图片管理',
      component: PictureManagePage,
    },
    {
      path: '/admin/spaceManage',
      name: '空间管理',
      component: SpaceManagePage,
    },
    {
      path: '/spaceUserManage/:id',
      name: '空间成员管理',
      component: SpaceUserManagePage,
      props: true,
    },
    {
      path: '/add_picture',
      name: '创建图片',
      component: AddPicturePage,
    },
    {
      path: '/add_picture/batch',
      name: '批量创建图片',
      component: AddPictureBatchPage,
    },
    {
      path: '/picture/:id',
      name: '图片详情',
      component: PictureDetailPage,
      props: true,
    },
    {
      path: '/add_space',
      name: '创建空间',
      component: AddSpacePage,
    },
    {
      path: '/community',
      name: '社区',
      component: () => import('@/pages/CommunityPage.vue'),
    },
    {
      path: '/community/discovery',
      name: '发现',
      component: () => import('@/pages/DiscoveryPage.vue'),
    },
    {
      path: '/community/createPost',
      name: '发布文章',
      component: () => import('@/pages/CommunityPostCreate.vue'),
    },
    {
      path: '/community/postDetail/:id',
      name: '文章详情',
      component: () => import('@/pages/CommunityPostDetail.vue'),
      props: true,
    },
    {
      path: '/chat-list',
      name: '聊天列表',
      component: () => import('@/pages/ChatListPage.vue'),
    },
    {
      path: '/time-line',
      name: '时间线',
      component: TimeLinePage,
    },
    {
      path: '/my_space',
      name: '我的空间',
      component: MySpacePage,
    },
    {
      path: '/space/:id',
      name: '空间详情',
      component: SpaceDetailPage,
      props: true,
    },
    {
      path: '/space_analyze',
      name: '空间分析',
      component: SpaceAnalyzePage,
    },
    {
      path: '/search_picture',
      name: '图片搜索',
      component: SearchPicturePage,
    },
    {
      path: '/user_exchange_vip',
      name: '用户兑换会员',
      component: UserExchangeVipPage,
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue'),
    },
  ],
})

// Add navigation guards to specific routes
router.beforeEach((to, from, next) => {
  const loginUserStore = useLoginUserStore()

  // 已经登录状态下访问登录页，重定向到首页
  if (to.path === '/user/login' && loginUserStore.loginUser.id) {
    next({ path: '/' })
    return
  }

  // 需要登录的路由列表
  const authRequiredRoutes = ['/forum', '/chat-list', '/message-center', '/my_space']

  // 检查路由是否需要登录且用户未登录
  if (authRequiredRoutes.includes(to.path) && !loginUserStore.loginUser.id) {
    // 重定向到登录页面，并保存目标URL
    next({
      path: '/user/login',
      query: { redirect: to.fullPath }
    })
  } else {
    // 继续导航
    next()
  }
})

export default router
